package com.lb.shopguide.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.TimeUtils;
import com.lb.shopguide.R;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.entity.GuideBalanceBean;
import com.lb.shopguide.entity.GuideMonthlyBalance;
import com.netease.nim.uikit.common.util.sys.TimeUtil;

import org.zakariya.stickyheaders.SectioningAdapter;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by kang on 2017/7/12.
 */

public class AdapterMyMoney extends SectioningAdapter {
    private Context mContext;
    private List<GuideMonthlyBalance> mList;

    public AdapterMyMoney(List<GuideMonthlyBalance> list, Context context) {
        mList = list;
        mContext = context;
    }

    @Override
    public void onBindItemViewHolder(SectioningAdapter.ItemViewHolder viewHolder, int sectionIndex, int itemIndex, int itemType) {
        GuideMonthlyBalance chargeBean = mList.get(sectionIndex);
        ItemViewHolder ivh = (ItemViewHolder) viewHolder;
        GuideBalanceBean memberBalanceInfoBean = chargeBean.getList().get(itemIndex);
        ivh.tvAction.setText(memberBalanceInfoBean.getInoutAccountTypeName());
        ivh.tvOutIn.setText("" + memberBalanceInfoBean.getInvolveMoney());

        Date date = TimeUtils.string2Date(memberBalanceInfoBean.getHappenTime());
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        if (TimeUtil.isSameDay(date, TimeUtils.getNowDate())) {
            ivh.tvDay.setText("今天");
            String hour = "" + calendar.get(Calendar.HOUR_OF_DAY);
            if (calendar.get(Calendar.HOUR_OF_DAY) < 10) {
                hour = "0" + hour;
            }
            String minute = "" + calendar.get(Calendar.MINUTE);
            if (calendar.get(Calendar.MINUTE) < 10) {
                minute = "0" + minute;
            }
            ivh.tvTime.setText(hour + ":" + minute);
        } else {
            ivh.tvDay.setText(TimeUtil.getWeekOfDate(date));
            ivh.tvTime.setText(calendar.get(Calendar.MONTH) + 1 + "-" + calendar.get(Calendar.DAY_OF_MONTH));
        }
        if (memberBalanceInfoBean.getInoutAccountTypeCode().equals(AppConstant.BALANCE_REWARD_MONEY)) {
            ivh.ivLogo.setImageResource(R.drawable.iv_reward);
        } else {
            ivh.ivLogo.setImageResource(R.drawable.iv_charge_return);
        }
        if (itemIndex == getNumberOfItemsInSection(sectionIndex)) {
            ivh.ivLine.setVisibility(View.GONE);
        } else {
            ivh.ivLine.setVisibility(View.VISIBLE);
        }
//        } else if (memberBalanceInfoBean.getInoutAccountTypeCode().equals(AppConstant.BALANCE_TYPE_GOODS_RETURN)
//                || memberBalanceInfoBean.getInoutAccountTypeCode().equals(AppConstant.BALANCE_TYPE_GOODS_RETURN_REDUCE)) {
//            ivh.ivLogo.setImageResource(R.drawable.iv_charge_return);
//        } else {
//            ImageLoaderUtils.displayCircle(mContext, ivh.ivLogo, memberBalanceInfoBean.getStoreLogoUrl());
//        }
    }

    @Override
    public void onBindHeaderViewHolder(SectioningAdapter.HeaderViewHolder viewHolder, int sectionIndex, int headerType) {
        GuideMonthlyBalance chargeBean = mList.get(sectionIndex);
        HeaderViewHolder hvh = (HeaderViewHolder) viewHolder;
        hvh.tvTitleTime.setText(chargeBean.getMonth());
    }

    public class ItemViewHolder extends SectioningAdapter.ItemViewHolder {
        TextView tvDay;
        TextView tvTime;
        TextView tvOutIn;
        TextView tvAction;
        ImageView ivLogo;
        View ivLine;

        public ItemViewHolder(View itemView) {
            super(itemView);
            tvDay = itemView.findViewById(R.id.tv_day);
            tvTime = itemView.findViewById(R.id.tv_time);
            tvOutIn = itemView.findViewById(R.id.tv_out_in);
            tvAction = itemView.findViewById(R.id.tv_action);
            ivLogo = itemView.findViewById(R.id.iv_logo);
            ivLine = itemView.findViewById(R.id.iv_line);
        }
    }

    public class HeaderViewHolder extends SectioningAdapter.HeaderViewHolder {
        TextView tvTitleTime;

        public HeaderViewHolder(View itemView) {
            super(itemView);
            tvTitleTime = itemView.findViewById(R.id.tv_time_title);
        }
    }

    @Override
    public ItemViewHolder onCreateItemViewHolder(ViewGroup parent, int itemType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.item_balance_record, parent, false);
        return new ItemViewHolder(v);
    }

    @Override
    public HeaderViewHolder onCreateHeaderViewHolder(ViewGroup parent, int headerType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.item_balance_title, parent, false);
        return new HeaderViewHolder(v);
    }

    @Override
    public int getNumberOfSections() {
        return mList.size();
    }

    @Override
    public int getNumberOfItemsInSection(int sectionIndex) {
        return mList.get(sectionIndex).getList().size();
    }

    @Override
    public boolean doesSectionHaveHeader(int sectionIndex) {
        return true;
    }

    @Override
    public boolean doesSectionHaveFooter(int sectionIndex) {
        return false;
    }
}
